package com.welsy.user.command
{
	import mx.rpc.IResponder;
	import com.adobe.cairngorm.commands.ICommand;
	import com.adobe.cairngorm.control.CairngormEvent;	
	import com.welsy.user.model.UserModelLocator;
	import com.welsy.util.Comparator;
	import mx.rpc.events.ResultEvent;
	import mx.rpc.events.FaultEvent;
	import com.welsy.user.business.WhoAmIDelegate;
	import mx.controls.Alert;
	import com.welsy.usermgt.vo.UserVO;
	import mx.core.Application;
	import com.welsy.user.event.WhoAmIEvent;		
	
	/**
	 * @version	$Revision: 1.1 $
	 */
	public class WhoAmICommand implements ICommand, IResponder
	{	
		private var applicationType : int
	
		public function execute( event : CairngormEvent ): void
		{		
			var authEvent : WhoAmIEvent = WhoAmIEvent(event);
			applicationType = authEvent.applicationType;				
			var delegate:WhoAmIDelegate = new WhoAmIDelegate( this );
			delegate.execute();					    			
		}
	
		public function result( event : Object ) : void
		{				
				
			var model : UserModelLocator = UserModelLocator.getInstance();
			//Alert.show("result: "+event.result);
			if (model.user == null) {
				var user : UserVO = new UserVO;
				var NS:Namespace = new Namespace("http://tempuri.org/UserMgtDataTypes.xsd");
				user.UserId = event.result..NS::UserId;	
				user.FirstName = event.result..NS::FirstName;
				user.LastName = event.result..NS::LastName;	
				user.Login = event.result..NS::Login
				user.RoleId = event.result..NS::RoleId;
				user.Email = event.result..NS::Email;
				model.user = user;			
				//Alert.show("RoleId: "+user.RoleId);
				
				//profilatura
				if (applicationType == WhoAmIEvent.ADMIN_APPLICATION) {
					//per admin application
					if (user.RoleId > 10) {												
						//Set permission into display views 
						mx.core.Application.application.enabled = true;
						switch( user.RoleId ) {	
							case 30: //su	
							case 28: //amministratore
								mx.core.Application.application.bodyPanel.statsmenu.enabled = true;	
								mx.core.Application.application.bodyPanel.newsmgt.enabled = true;																					
							case 25: //validatore
								mx.core.Application.application.bodyPanel.iter.enabled = true;
								mx.core.Application.application.bodyPanel.usermgt.enabled = true;
								mx.core.Application.application.bodyPanel.classroom.enabled = true;
							case 23: //autore
							case 20: //autore esterno
								mx.core.Application.application.bodyPanel.authoring.enabled = true;
								break;										
						}	
						
					} else {
						Alert.show( "L'utente non è un amministratore del sistema" );
						return;
					}
				}
				
				if (applicationType == WhoAmIEvent.USER_APPLICATION) {
					//per user application
					if (user.RoleId > 0) {
						mx.core.Application.application.enabled = true;
					} else {
						Alert.show( "Utente non autorizzato" );
						return;
					}
				}									
				
				//Setup the authorId for te current view
				if(user.RoleId > 20) {
					UserModelLocator.getInstance().authorId = 0;
				} else {
					UserModelLocator.getInstance().authorId = int(user.UserId);
				}		
			}
			else
			{
				Alert.show( "User already retrieved!" );
				return;
			}
		}
				
		public function fault( event : Object ) : void
		{
			var faultEvent : FaultEvent = FaultEvent( event );
			Alert.show("Utente non autorizzato" );
			Alert.show(faultEvent.fault.toString());
		}
	}

}